package com.wwwarehouse.resource_center.utils;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;

import com.wwwarehouse.common.bean.upload.FileUploadBean;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.eventbus_event.UploadSuccessEvent;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ThreadUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Description: 上传图片Utils
 * Copyright  : Copyright (c) 2017
 * Author     : yfwang
 * Date       : 2017/9/25 19:18
 */
public class UploadPicUtil {
    private static UploadPicUtil instance;

    private UploadPicUtil() {

    }
    public static synchronized UploadPicUtil getInstance(){
        if (instance==null){
            instance = new UploadPicUtil();
        }
        return instance;
    }

     //单张图片上传
    public  void uploadPic(String path, final File mFile, final Activity mActivity){
        List<String>  selectedImageList = new ArrayList<>();
        selectedImageList.add(path);

        String uri = Constant.URL_UPLOADS_ITEM;
        NoHttpUtils.httpUploadFileRequest(uri, selectedImageList, new NoHttpUtils.OnFileUploadListener() {
            @Override
            public void onStart(int position) {

            }

            @Override
            public void onCancel(int position) {

            }

            @Override
            public void onProgress(int position, int progress) {
            }

            @Override
            public void onFinish(int position) {


                //上传完成
                ThreadUtils.newThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mFile != null) {
                            // 把文件插入到系统图库
                            try {
                                 MediaStore.Images.Media.insertImage(mActivity.getContentResolver(), mFile.getAbsolutePath(), mFile.getName(), null);
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                            // 通知图库更新
                            mActivity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + mFile.getAbsolutePath())));

                            //删除文件
                            mFile.delete();
                        }
                    }
                });
            }

            @Override
            public void onError(int position, Exception exception) {
            }
        }, new NoHttpUtils.OnFileUploadResultListener() {
            @Override
            public void onSucceed(int what, FileUploadBean fileUploadBean) {
                EventBus.getDefault().post(new UploadSuccessEvent(fileUploadBean));
            }

            @Override
            public void onFailed(int what, FileUploadBean fileUploadBean) {
            }
        });


    }


}
